package org.apache.dolphinscheduler.api.controller;

import static org.apache.dolphinscheduler.api.enums.Status.QUERY_USER_PAY_FAIL;
import static org.apache.dolphinscheduler.api.enums.Status.RECHAGE_USER_PAY_FAIL;

import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.UserPayDetailService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.Map;

@Tag(name = "USER_PAY_TAG")
@RestController
@RequestMapping("/user-pay")
public class UserPayController extends BaseController {

	@Autowired
	private UserPayDetailService userPayDetailService;

	@Operation(summary = "recharge", description = "RECHARGE")
    @PostMapping(value = "/recharge/{recharge_apply_id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(RECHAGE_USER_PAY_FAIL)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    public Result<Object> rechage(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
    		@PathVariable("recharge_apply_id") Integer rechargeApplyId) {
        return userPayDetailService.rechage(loginUser, rechargeApplyId);
    }

	@Operation(summary = "orderPay", description = "ORDERPAY")
    @PostMapping(value = "/orderPay/{order_id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(RECHAGE_USER_PAY_FAIL)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    public Result<Object> orderPay(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
    		@PathVariable("order_id") Integer orderId,
        @RequestBody Map<String, Integer> body) {
        Integer couponbillId = body.get("couponbill_id");
        return userPayDetailService.orderPay(loginUser, orderId, couponbillId);
    }

	@Operation(summary = "queryUserPayList", description = "QUERY_USER_PAY_LIST_NOTES")
    @Parameters({
            @Parameter(name = "pageNo", description = "PAGE_NO", required = true, schema = @Schema(implementation = int.class, example = "1")),
            @Parameter(name = "pageSize", description = "PAGE_SIZE", required = true, schema = @Schema(implementation = int.class, example = "10")),
            @Parameter(name = "searchVal", description = "SEARCH_VAL", schema = @Schema(implementation = String.class))
    })
    @GetMapping(value = "/list")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_USER_PAY_FAIL)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    public Result<Object> queryUserGroupList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                @RequestParam("pageNo") Integer pageNo,
                                @RequestParam("pageSize") Integer pageSize,
                                @RequestParam(value = "searchVal", required = false) String searchVal) {
        Result<Object> result = checkPageParams(pageNo, pageSize);
        if (!result.checkResult()) {
            return result;
        }
        searchVal = ParameterUtils.handleEscapes(searchVal);
        return userPayDetailService.queryPayDetail(loginUser, pageNo, pageSize, searchVal);
    }
}
